<script type="text/javascript">
function explode( delimiter, string, limit ) {
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}// }}}

var old_id=0;
var arr_list_cat=new Array;
var pre = new Array;
var pre_id;
var arr_old_id=new Array;
var yahoo_id;
var cat_name;
var orgColor;
var orgBack;
var Color="white";
var Back="#0099FF";
var arr_list_remove_cat=new Array;
//ID:div_id to remove
//CAT ID =category id
//RUN ID =row id in div
function discardElement(element) {
         var garbageBin = document.getElementById('IELeakGarbageBin');
         if (!garbageBin) {
                 garbageBin = document.createElement('DIV');
                 garbageBin.id = 'IELeakGarbageBin';
                 garbageBin.style.display = 'none';
                 document.body.appendChild(garbageBin);
         }
         // move the element to the garbage bin
         garbageBin.appendChild(element);
         garbageBin.innerHTML = '';
}

function HighLightTR(value)
{
var arr=explode(',',value);
id=arr[0];
cat_id=arr[1];
run_id=arr[2];
cat_yahoo_id=arr[3];
cat_name=arr[4];

yahoo_id=cat_yahoo_id;
document.getElementById('selected_level').value=id;
document.getElementById('cat_id').value=cat_id;
document.getElementById('cat_name').value=cat_name;
var parent=document.getElementById("root");
if(id==0)
{
	if(arr_list_cat=='' || arr_list_cat==null)
	{
		var next_id=parseInt(id)+1;
		var child_div=document.getElementById(next_id);
		
		if(child_div != null)
		//parent.removeChild(child_div);
		discardElement(child_div);
		
	}
	
	else
	{
		var arr_lenght=arr_list_cat.length;
	
		for (var i=1;i<=arr_lenght;i++) 
		{
			var next_id=parseInt(i);
			var child_div=document.getElementById(next_id);
			
			if(child_div != null)
			discardElement(child_div);
			
		}
	}
	
	
}
//*******************************************************************************************/
else
{			
			var obj=document.getElementById(run_id);
			
			//REMOVE DIV
			var arr_lenght=arr_list_cat.length;
			for (var i=1; i<=arr_lenght; i++) 
			{
				var next_id=parseInt(id)+parseInt(i);
				var child_div=document.getElementById(next_id);
				if(child_div != null)
				parent.removeChild(child_div);
			}

}

doListChild(cat_id);
old_id=id;
/////////////////////////////////////////////

//////////////////////////////////
}

function doListParent()
{
		display("Loading...","ajax_loading2");
		oXMLHttp = checkXMLHTTP();
		var setRequest = typeof(oXMLHttp.setRequestHeader);
		if ( oXMLHttp && setRequest!="undefined" )
		{
			var myQuery = "";	
			orderPost( oXMLHttp, "{URL_SITE}admin/?sMod=groupcategory&sAction=list_parent_category", myQuery, HTTPshowListParent, true );
		}
}
function HTTPshowListParent()
{
   if ( typeof(oXMLHttp)==='undefined' )
       return;
    // Return status of XMLHttp object
    if ( oXMLHttp.readyState==4 )
    {
        // if oXMLHttp.status is OK
        if ( oXMLHttp.status==200 )
        {
			sRes = ((oXMLHttp.responseText));
			oJson = sRes.parseJSON(); 			
			 if ( typeof( oJson ) === 'object' )
                {
						display("&nbsp;","ajax_loading2");
						var result=oJson['BODY']['RES'];
						if(result!='')
						{
								
								var now_html=document.getElementById("root").innerHTML;
								var test=now_html+result;
								display(test,'root');
								document.getElementById('selected_level').value=0;
						}
				}	
		}
        if ( oXMLHttp.status==404)
        {
            alert("Page not found");
        }
    }

}

function doListChild(id)
{
		if(id=='')
		return;
		display("Loading...","ajax_loading2");
		oXMLHttp = checkXMLHTTP();
		var setRequest = typeof(oXMLHttp.setRequestHeader);
		var cur_level=document.getElementById('selected_level').value;
		if ( oXMLHttp && setRequest!="undefined" )
		{
			var myQuery = "&parent_id="+id+"&current_level="+cur_level;	
			orderPost( oXMLHttp, "{URL_SITE}admin/?sMod=groupcategory&sAction=list_child_category", myQuery, HTTPshowListChild, true );
		}
}
function HTTPshowListChild()
{
   if ( typeof(oXMLHttp)==='undefined' )
       return;
    // Return status of XMLHttp object
    if ( oXMLHttp.readyState==4 )
    {
        // if oXMLHttp.status is OK
        if ( oXMLHttp.status==200 )
        {
			sRes = ((oXMLHttp.responseText));
			
			oJson = sRes.parseJSON(); 			
			 if ( typeof( oJson ) === 'object' )
                {
						//var now_id=click_one++;
						//addColumn('list_cat',now_id);
						display("&nbsp;","ajax_loading2");
						var result=oJson['BODY']['RES'][0]; //RESULT TEXT
						var div_id=oJson['BODY']['RES'][1];	//RESULT ID
						var txt_yahoo_id=document.getElementById('yahoo_id');
						
						if(result!='')
						{
								
								//var now_html=document.getElementById("root").innerHTML;
								//var test=now_html+result;
								//display(test,"root");
								
								var obj_div=document.getElementById(div_id);
								//IF DIV IS NOT EXITS,CREATE IT
								if(obj_div==null)
								{
									var div = document.createElement('div');
									div.setAttribute('name', 'list_cat');
									div.setAttribute('id',div_id);					
									document.getElementById("root").appendChild(div);
									display(result,div_id);

								}
								else
								{
									display(result,div_id);
								}
								//arr_list_cat=document.getElementsByName('list_cat[]');
								arr_list_cat=getElementsByName_iefix('div','list_cat');
								txt_yahoo_id.value='';
						}
						else
						{
							txt_yahoo_id.value=yahoo_id;
						}
				}	
		}
        if ( oXMLHttp.status==404)
        {
            alert("Page not found");
        }
    }

}

function getElementsByName_iefix(tag, name) {
     
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function add_cat()
{
    var cat_id=document.getElementById('cat_id').value;
	if(cat_id=='')
	{
		alert("{GROUPCATEGORY.select_category}");
		return false;
	}
	else
	{
		add_option(cat_id,document.getElementById('cat_name').value);
		
	}
}

function remove_cat()
{
	var j=0;
	var obj_select=document.getElementById("display_group_child");
	var list_cast=document.getElementById('list_cats');
	list_cast.value='';
	for(var i=0;i<obj_select.length;i++)
	{
		if(obj_select.options[i].selected==true)
		{
			arr_list_remove_cat[j]=obj_select.options[i].value;
			j++;
		}
	}
	
	var arr_length=arr_list_remove_cat.length;
	if(arr_length==0)
	{
		alert('{GROUPCATEGORY.select_category}');
		return;
	}
	
	for(var t=0;t<arr_length;t++)
	{
		//REMOVE FROM SELECTE BOX
		for(var f=0;f<obj_select.length;f++)
		{
			if(obj_select.options[f].value==arr_list_remove_cat[t])
			{
				obj_select.removeChild(obj_select.options[f]);
			}
		}
	}
	
	//
	for(var f=0;f<obj_select.length;f++)
		{
			list_cast.value+=" "+obj_select[f].value;
		}
	
}

function add_option(value,text)
{
	var obj_select=document.getElementById("display_group_child");
	
	for(var i=0;i<obj_select.length;i++)
	{
		if(obj_select.options[i].value==value)
		{
			alert('E This category already in list');
			return;
		}
	}
	
		
	var newOption = document.createElement('<option value="'+value+'">');
    obj_select.options.add(newOption);
    newOption.innerText = text;
	document.getElementById('group_child').value+=" "+value;
    //newOption.selected="selected";
}

//SUBMIT FORM
function check_form()
{
	display('&nbsp;','alert_cat_name');
	var frm_name=document.getElementById("frm_groupcat_new");
	var aCheck = new Array(); 
	aCheck.push ( new Array('group_name', 1, 200, "{GROUPCATEGORY.input_group_name}", false, false,'alert_cat_name') );
	var bRes = validateFields(aCheck);
	
	if(!bRes)
	{
		return;
	}
	else
	{		
			frm_name.submit();
	}		
	
	
}

</script>